package addressBook;

import javax.swing.DefaultListModel;

public class contacts {


	int maxContacts;
	private int numContacts;
	String[][] contacts;
	DefaultListModel listModel;
	private int arg=1;

	public contacts(int max) {
    	maxContacts = max;
    	setListModel(listModel);
		setContacts(contacts = new String[maxContacts][7]);
		this.numContacts = 0;
    }
	
	public void addContact(contact c) {
		boolean bool = searchContact(c);
		if(!bool){
			this.getContacts()[numContacts()][0] = c.getFirstName();
			this.getContacts()[numContacts()][1] = c.getLastName();
			this.getContacts()[numContacts()][2] = c.getPersoHomeNum();
			this.getContacts()[numContacts()][3] = c.getAddress();
			this.getContacts()[numContacts()][4] = c.getCity();
			this.getContacts()[numContacts()][5] = c.getUrl();
			this.getContacts()[numContacts()][6] = c.getEmail();
	    	this.listModel.addElement(c.getFirstName().concat(" ").concat(c.getLastName()));
			this.numContacts++;
		}
    }
	
	public void modifyContact(Object elem, int ind) {
		getListModel().setElementAt(elem, ind);
    }
	
	public void removeContact(contact c){
		boolean bool = searchContact(c);
		int id = idContact(c);
		if(bool){
			int i;
			for (i=id; i<numContacts(); i++){
				this.getContacts()[i][0] = this.getContacts()[i+1][0];
				this.getContacts()[i][1] = this.getContacts()[i+1][1];
				this.getContacts()[i][2] = this.getContacts()[i+1][2];
				this.getContacts()[i][3] = this.getContacts()[i+1][3];
				this.getContacts()[i][4] = this.getContacts()[i+1][4];
				this.getContacts()[i][5] = this.getContacts()[i+1][5];
				this.getContacts()[i][6] = this.getContacts()[i+1][6];
			}
			this.listModel.removeElement(c.getFirstName().concat(" ").concat(c.getLastName()));
			this.numContacts--;
		}
	}
	
	public boolean searchContact(contact c){
		boolean search=false;
		for (int i=0; i<numContacts(); i++){
			boolean yes=false;
			int j=0;
			switch(j){
			case 0:
					if(this.getContacts()[i][j].equals(c.getFirstName())){
						j++;
					}
					else{
						search=false;
						break;
					}
			case 1:
					if(this.getContacts()[i][j].equals(c.getLastName())){
						j++;
					}
					else{
						search=false;
						break;
					}
			case 2:
					if(this.getContacts()[i][j].equals(c.getPersoHomeNum()) || c.getPersoHomeNum().isEmpty()){
						j++;
					}
					else{
						search=false;
						break;
					}
			case 3:
					if(this.getContacts()[i][j].equals(c.getAddress()) || c.getAddress().isEmpty()){
						j++;
					}
					else{
						search=false;
						break;
					}
			case 4:
					if(this.getContacts()[i][j].equals(c.getCity()) || c.getCity().isEmpty()){
						j++;
					}
					else{
						search=false;
						break;
					}
			case 5:
					if(this.getContacts()[i][j].equals(c.getUrl()) || c.getUrl().isEmpty()){
						j++;
					}
					else{
						search=false;
						break;
					}
			case 6:
					if(this.getContacts()[i][j].equals(c.getEmail()) || c.getEmail().isEmpty()){
						search=true;
						yes=true;
						break;
					}
					else{
						search=false;
						break;
					}
			}
			if(yes==true)
				break;
		}
		return search;
	}
	
	public int idContact(contact c){
		int id = numContacts();
		for (int i=0; i<numContacts(); i++){
			boolean yes=false;
			int j=0;
			switch(j){
			case 0:
					if(this.getContacts()[i][j].equals(c.getFirstName())){
						j++;
					}
					else{
						break;
					}
			case 1:
					if(this.getContacts()[i][j].equals(c.getLastName())){
						j++;
					}
					else{
						break;
					}
			case 2:
					if(this.getContacts()[i][j].equals(c.getPersoHomeNum()) || c.getPersoHomeNum().isEmpty()){
						j++;
					}
					else{
						break;
					}
			case 3:
					if(this.getContacts()[i][j].equals(c.getAddress()) || c.getAddress().isEmpty()){
						j++;
					}
					else{
						break;
					}
			case 4:
					if(this.getContacts()[i][j].equals(c.getCity()) || c.getCity().isEmpty()){
						j++;
					}
					else{
						break;
					}
			case 5:
					if(this.getContacts()[i][j].equals(c.getUrl()) || c.getUrl().isEmpty()){
						j++;
					}
					else{
						break;
					}
			case 6:
					if(this.getContacts()[i][j].equals(c.getEmail()) || c.getEmail().isEmpty()){
						id=i;
						yes=true;
						break;
					}
					else{
						break;
					}
			}
			if(yes==true)
				break;
		}
		return id;
	}
	
	public void sortContacts(int arg) {
		this.arg = arg;
		String[][] sortedContacts = new String[maxContacts][5];
		String[] buffer;
		sortedContacts = getContacts();
		int j = 0;
		do{
			for (int i=0; i<numContacts()-1; i++){
				if(sortedContacts[i][arg].compareTo(sortedContacts[i+1][arg])>0){
					buffer=sortedContacts[i];
					sortedContacts[i]=sortedContacts[i+1];
					sortedContacts[i+1]=buffer;
				}
				else if(sortedContacts[i][arg].compareTo(sortedContacts[i+1][arg])==0&&arg==0){
					if(sortedContacts[i][1].compareTo(sortedContacts[i+1][1])>0){
						buffer=sortedContacts[i];
						sortedContacts[i]=sortedContacts[i+1];
						sortedContacts[i+1]=buffer;
					}
					else if(sortedContacts[i][2].compareTo(sortedContacts[i+1][2])==0&&arg==1){
						if(sortedContacts[i][arg+2].compareTo(sortedContacts[i+1][arg+2])>0){
							buffer=sortedContacts[i];
							sortedContacts[i]=sortedContacts[i+1];
							sortedContacts[i+1]=buffer;
						}
						else if(sortedContacts[i][arg+2].compareTo(sortedContacts[i+1][arg+2])==0&&arg==2){
							if(sortedContacts[i][arg+3].compareTo(sortedContacts[i+1][arg+3])>0){
								buffer=sortedContacts[i];
								sortedContacts[i]=sortedContacts[i+1];
								sortedContacts[i+1]=buffer;
							}
							else if(sortedContacts[i][arg+3].compareTo(sortedContacts[i+1][arg+3])==0&&arg==3){
								if(sortedContacts[i][arg+4].compareTo(sortedContacts[i+1][arg+4])>0){
									buffer=sortedContacts[i];
									sortedContacts[i]=sortedContacts[i+1];
									sortedContacts[i+1]=buffer;
								}
								else if(sortedContacts[i][arg+4].compareTo(sortedContacts[i+1][arg+4])==0&&arg==4){
									if(sortedContacts[i][arg+5].compareTo(sortedContacts[i+1][arg+5])>0){
										buffer=sortedContacts[i];
										sortedContacts[i]=sortedContacts[i+1];
										sortedContacts[i+1]=buffer;
									}
								}
							}
						}
					}
				}
				else if(sortedContacts[i][arg].compareTo(sortedContacts[i+1][arg])==0&&(arg==1||arg==2||arg==3||arg==4||arg==5||arg==6)){
					if(sortedContacts[i][0].compareTo(sortedContacts[i+1][0])>0){
						buffer=sortedContacts[i];
						sortedContacts[i]=sortedContacts[i+1];
						sortedContacts[i+1]=buffer;
					}
					else if(sortedContacts[i][0].compareTo(sortedContacts[i+1][0])==0){
						if(sortedContacts[i][1].compareTo(sortedContacts[i+1][1])>0){
							buffer=sortedContacts[i];
							sortedContacts[i]=sortedContacts[i+1];
							sortedContacts[i+1]=buffer;
						}
						else if(sortedContacts[i][1].compareTo(sortedContacts[i+1][1])==0){
							if(sortedContacts[i][2].compareTo(sortedContacts[i+1][2])>0){
								buffer=sortedContacts[i];
								sortedContacts[i]=sortedContacts[i+1];
								sortedContacts[i+1]=buffer;
							}
							else if(sortedContacts[i][2].compareTo(sortedContacts[i+1][2])==0){
								if(sortedContacts[i][3].compareTo(sortedContacts[i+1][3])>0){
									buffer=sortedContacts[i];
									sortedContacts[i]=sortedContacts[i+1];
									sortedContacts[i+1]=buffer;
								}
								else if(sortedContacts[i][3].compareTo(sortedContacts[i+1][3])==0){
									if(sortedContacts[i][4].compareTo(sortedContacts[i+1][4])>0)
										sortedContacts[i]=sortedContacts[i+1];
									else if(sortedContacts[i][4].compareTo(sortedContacts[i+1][4])==0){
										buffer=sortedContacts[i];
										sortedContacts[i]=sortedContacts[i+1];
										sortedContacts[i+1]=buffer;
									}
										if(sortedContacts[i][5].compareTo(sortedContacts[i+1][5])>0){
											buffer=sortedContacts[i];
											sortedContacts[i]=sortedContacts[i+1];
											sortedContacts[i+1]=buffer;
										}
										else if(sortedContacts[i][5].compareTo(sortedContacts[i+1][5])==0&&arg==4){
											if(sortedContacts[i][6].compareTo(sortedContacts[i+1][6])>0){
												buffer=sortedContacts[i];
												sortedContacts[i]=sortedContacts[i+1];
												sortedContacts[i+1]=buffer;
											}
										}
								}
							}
						}
					}
				}
							
			}
			j++;
		}while(j != numContacts());
		setContacts(sortedContacts);
		resetList();
    }
	
	public void resetList(){
		listModel.removeAllElements();
		for (int i=0; i<numContacts(); i++){
			listModel.addElement(getContacts()[i][0].toString().concat(" ").concat(getContacts()[i][1].toString()));
		}
	}
	
    public int numContacts() {
		return numContacts;
    }

	public String[][] getContacts() {
		return contacts;
	}

	public void setListModel(DefaultListModel listModel) {
		this.listModel = listModel;
	}
	
	public DefaultListModel getListModel() {
		return listModel;
	}

	public void setContacts(String[][] contacts) {
		this.contacts = contacts;
	}
	
	public void testContactsClass() {
		System.out.print(contacts[0][0]);
		System.out.print(' ');
		System.out.print(contacts[0][1]);
		System.out.println();
	}
	
    public int getArg() {
		return arg;
	}

	public void setArg(int arg) {
		this.arg = arg;
	}
    
}
